package org.gw.building.ctrl;

import org.gw.building.util.Constant;
import org.gw.ylc.base.model.Administrator;
import org.gw.ylc.base.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("admin")
public class AdminCtrl {
	@Autowired
	private AdministratorService administratorService;

	@RequestMapping("query")
	@ResponseBody
	public Object query(String name, String department, Integer type) {
		return administratorService.query(name, department, type, Constant.app);
	}

	@RequestMapping("add")
	@ResponseBody
	public Object add(Administrator administrator) {
		administrator.setApp(Constant.app);
		administratorService.add(administrator);
		return "succ";
	}

	@RequestMapping("update")
	@ResponseBody
	public Object update(Administrator administrator) {
		administrator.setApp(Constant.app);
		administratorService.update(administrator);
		return "succ";
	}

	@RequestMapping("delete")
	@ResponseBody
	public Object delete(String id) {
		administratorService.delete(id, Constant.app);
		return "succ";
	}

	@RequestMapping("photo/{gh}")
	@ResponseBody
	public void queryStudent(@PathVariable String gh, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String webRoot = System.getProperty("train.root");
		File photo = new File(webRoot + "/asset/images/touxiang.png");
		OutputStream out = response.getOutputStream();
		FileCopyUtils.copy(new FileInputStream(photo), out);
		out.flush();
	}
}
